﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CommonUtil
{
    public class FileSendStatus
    {
        private FileInfo info;
        private long fileBytes;

        public FileSendStatus(string filePath)
        {
            info = new FileInfo(filePath);
            fileBytes = info.Length;
        }

        public string PrintStatus(int sent)
        {
            string percent = GetPercent(sent);
            return string.Format("已发送{0}字节，当前{1}%...", sent, percent);
        }

        public string GetPercent(int sent)
        {
            decimal allBytes = Convert.ToDecimal(fileBytes);
            decimal currentSent = Convert.ToDecimal(sent);
            decimal percent = (currentSent / allBytes) * 100;
            percent = Math.Round(percent, 1);   //保留一位小数
            if (percent.ToString() == "100.0")
                return "100";
            else
                return percent.ToString();
        }
    }
}
